package com.lkd.handler;

import com.google.common.base.Strings;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 接收完成工单消息，更改售货机状态
 */
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompleteMsgHandler implements MsgHandler {

    @Resource
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {

        //解析消息转换成协议类
        TaskCompleteContract task = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);

        //判断为空直接返回
        if (task == null || Strings.isNullOrEmpty(task.getInnerCode())) {
            return;
        }

        //如果是投放工单，将售货机状态改为运营
        if (task.getTaskType() == VMSystem.TASK_TYPE_DEPLOY) {
            vendingMachineService.updateStatus(task.getInnerCode(), VMSystem.VM_STATUS_RUNNING);
        }

        //如果是撤机工单，将售货机状态改为撤机
        if (task.getTaskType() == VMSystem.TASK_TYPE_REVOKE) {
            vendingMachineService.updateStatus(task.getInnerCode(), VMSystem.VM_STATUS_REVOKE);
        }

    }
}
